<?php 

function cc_msubstr($str, $start=0, $length = 4, $charset="utf-8", $suffix=true)
{
	if(function_exists("mb_substr"))
		return mb_substr($str, $start, $length, $charset);
	elseif(function_exists('iconv_substr')) {
		return iconv_substr($str,$start,$length,$charset);
	}
	$re['utf-8']   = "/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|[/xe0-/xef][/x80-/xbf]{2}|[/xf0-/xff][/x80-/xbf]{3}/";
	$re['gb2312'] = "/[/x01-/x7f]|[/xb0-/xf7][/xa0-/xfe]/";
	$re['gbk']	  = "/[/x01-/x7f]|[/x81-/xfe][/x40-/xfe]/";
	$re['big5']	  = "/[/x01-/x7f]|[/x81-/xfe]([/x40-/x7e]|/xa1-/xfe])/";
	preg_match_all($re[$charset], $str, $match);
	$slice = join("",array_slice($match[0], $start, $length));
	if($suffix) return $slice."…";
	return $slice;
}




function strLength($str,$charset='utf-8'){
if($charset=='utf-8') $str = iconv('utf-8','gb2312',$str);
$num = strlen($str);
$cnNum = 0;
for($i=0;$i<$num;$i++){
if(ord(substr($str,$i+1,1))>127){
$cnNum++;
$i++;
}
}
$enNum = $num-($cnNum*2);
$number = ($enNum/2)+$cnNum;
return ceil($number);
}




    function getpath($url,$filename,$a='d'){
      switch($a){
        case 'a':
          $pap = '30_';
          break;
        case 'b':
          $pap = '50_';
          break;
        case 'c':
          $pap = '140_';
          break;
        case 'd': 
          $pap = null;
          break;
      }
      $img_path = $url.'uploads/';
      $img_path .= substr($filename, 0, 4).'/';
      $img_path .= substr($filename, 4, 2).'/';
      $img_path .= substr($filename, 6, 2).'/';
      $img_path .= $pap.$filename;
      return $img_path;
  } 



  function NSLog($v){

    if (is_array($v)) {
        
        echo "<pre>";
        print_r($v);
        echo "<pre/>";
        
    }else{

      echo "<pre>";
        var_dump($v);
        echo "<pre/>";
    }

  }




    function random($length, $numeric = 0)
  {
    $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
    $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
    if($numeric)
    {
      $hash = '';
    }
    else
    {
      $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
      $length--;
    }
    $max = strlen($seed) - 1;
    for($i = 0; $i < $length; $i++)
    {
      $hash .= $seed{mt_rand(0, $max)};
    }
    return $hash;
  }
  
  function fileext($filename)
  {
    return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  }
	







/** curl 获取 https 请求 
* @param String $url        请求的url 
* @param Array  $data       要發送的數據 
* @param Array  $header     请求时发送的header 
* @param int    $timeout    超时时间，默认30s 
*/  
function curl_https($url, $data=array(), $header=array(), $timeout=30){  
  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
  
    $response = curl_exec($ch);  
  
    if($error=curl_error($ch)){  
        die($error);  
    }  
  
    curl_close($ch);  
  
    return $response;  
  
}  



function   get_week($date){
        //强制转换日期格式
        $date_str=date('Y-m-d',strtotime($date));
   
        //封装成数组
        $arr=explode("-", $date_str);
        
        //参数赋值
        //年
        $year=$arr[0];
        
        //月，输出2位整型，不够2位右对齐
        $month=sprintf('%02d',$arr[1]);
        
        //日，输出2位整型，不够2位右对齐
        $day=sprintf('%02d',$arr[2]);
        
        //时分秒默认赋值为0；
        $hour = $minute = $second = 0;   
        
        //转换成时间戳
        $strap = mktime($hour,$minute,$second,$month,$day,$year);
        
        //获取数字型星期几
        $number_wk=date("w",$strap);
        
        //自定义星期数组
        $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
        
        //获取数字对应的星期
        return $weekArr[$number_wk];
    }



  function EAN_13($code) {
  //一个单元的宽度
  $lw = 2;
  //条码高 
  $hi = 100;
  // the guide code is no coding,is used to show the left part coding type//
  // Array guide is used to record the EAN_13 is left part coding type//
  $Guide = array(1=>'AAAAAA','AABABB','AABBAB','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');
  $Lstart ='101';
  $Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'),
                   "B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'));
  $Rencode = array('1110010','1100110','1101100','1000010','1011100',
                   '1001110','1010000','1000100','1001000','1110100');    

  $center = '01010';

  $ends = '101';
  if ( strlen($code) != 13 )
   { die("UPC-A Must be 13 digits."); }
$lsum =0;
$rsum =0;
  for($i=0;$i<(strlen($code)-1);$i++)
  {
    if($i % 2)
{
 // $odd += $ncode[$x]
  $lsum +=(int)$code[$i];
 }else{
  $rsum +=(int)$code[$i];
 }

  }
  $tsum = $lsum*3 + $rsum;
    if($code[12] != (10-($tsum % 10)))
{
   die("the code is bad!");
    } 
 // echo $Guide[$code[0]];
  $barcode = $Lstart;
  for($i=1;$i<=6;$i++)
  {
    $barcode .= $Lencode [$Guide[$code[0]][($i-1)]] [$code[$i]];
  }
  $barcode .= $center;

  for($i=7;$i<13;$i++)
  {
    $barcode .= $Rencode[$code[($i)]] ;
  }
  $barcode .= $ends;

    $img = ImageCreate($lw*95+60,$hi+30);
  $fg = ImageColorAllocate($img, 0, 0, 0);
  $bg = ImageColorAllocate($img, 255, 255, 255);
  ImageFilledRectangle($img, 0, 0, $lw*95+60, $hi+30, $bg);
  $shift=10;
  for ($x=0;$x<strlen($barcode);$x++) {
    if (($x<4) || ($x>=45 && $x<50) || ($x >=92)) 
  { 
    $sh=10; 
  } else { 
    $sh=0; 
  }
    if ($barcode[$x] == '1') 
{ 
  $color = $fg;
    } else { 
  $color = $bg; 
}
    ImageFilledRectangle($img, ($x*$lw)+30,5,($x+1)*$lw+29,$hi+5+$sh,$color);
  }
  /* Add the Human Readable Label */
  ImageString($img,5,20,$hi+5,$code[0],$fg);
  for ($x=0;$x<6;$x++) {
    ImageString($img,5,$lw*(8+$x*6)+30,$hi+5,$code[$x+1],$fg);
    ImageString($img,5,$lw*(53+$x*6)+30,$hi+5,$code[$x+7],$fg);
  }
 // ImageString($img,4,$lw*95+17,$hi-5,$code[12],$fg);
  /* Output the Header and Content. */
  // header("Content-Type: image/png");
 return imagepng ($img, '/');

}
// EAN_13('6901028055048');

 function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}




 function mutil($a,$b){

 	return $a*$b;
 }


 function qrcode($url,$size=4){
    Vendor('Phpqrcode.phpqrcode');
    // 如果没有http 则添加
    if (strpos($url, 'http')===false) {
        $url='http://'.$url;
    }
    QRcode::png($url,false,QR_ECLEVEL_L,$size,2,false,0xFFFFFF,0x000000);
}